/*Lupton Teigen 2025 "Base Assumptions"
This script does not generate a table or figure in the manuscript, rather it compiles county-level data to test the idea that *Chinni and Gimpel's "Military Bastion" counties are more Republican than average. See first paragraph of "Results" section.
*/

*pathname needs customization
local destfold "E:\Dropbox\Data\county\"

clear
import delimited "`destfold'cc-est2022-all.csv", delimiter("", collapse) 
*https://www2.census.gov/programs-surveys/popest/datasets/2020-2022/counties/asrh/

keep if agegrp==0
keep if year==2
keep state county tot_pop ba_male ba_female aac_male aac_female nh_male nhwa_male nhwa_female nhaa_male nhaa_female nh_female h_male h_female hwa_male hwa_female stname ctyname

gen fipscode = (state*1000)+county
format %05.0f fipscode
set seed 2234
gen rando =runiform()
/*make sure the fips code thing worked */
list state county fipscode stname ctyname if rando<0.05 
gen blackperc = (ba_male+ba_female)/tot_pop*100 
gen whiteperc = (nhwa_male+nhwa_female)/tot_pop*100
gen asianperc = (nhaa_male+nhaa_female)/tot_pop*100
gen hispperc = (h_male+h_female)/tot_pop*100
list fipscode stname ctyname blackperc whiteperc asianperc hispperc if rando<0.10

rename stname cc_stname 
rename ctyname cc_ctyname
keep fipscode cc_ctyname cc_stname blackperc whiteperc asianperc hispperc 
saveold "`destfold'cleanedup_race_eth_data_acs2020.dta", version(13) replace 

clear
import delimited "`destfold'countypres_2000-2020.csv", delimiter("", collapse) 
keep if year==2020
keep if party=="DEMOCRAT" | party=="REPUBLICAN"

egen allvotes = total(candidatevotes), by(candidate county_fips)
keep state_po county_name county_fips candidate allvotes
duplicates drop
keep if candidate=="DONALD J TRUMP"
sum
rename allvotes trump2020
rename state_po trumpst
rename county_name trumpcty
drop candidate
destring county_fips, gen(fipscode)
format %05.0f fipscode
sort fipscode
save "`destfold'trumpbycounty2020.dta" , replace

clear
import delimited "`destfold'countypres_2000-2020.csv", delimiter("", collapse) 
keep if year==2020
keep if party=="DEMOCRAT" | party=="REPUBLICAN"

egen allvotes = total(candidatevotes), by(candidate county_fips)
keep state_po county_name county_fips candidate allvotes
duplicates drop
keep if candidate=="JOSEPH R BIDEN JR"
sum
rename allvotes biden2020
rename state_po bidenst
rename county_name bidencty
drop candidate
destring county_fips, gen(fipscode)
format %05.0f fipscode
sort fipscode
save "`destfold'bidenbycounty2020.dta" , replace

clear
use "`destfold'cleanedup_race_eth_data_acs2020.dta"
sort fipscode
merge 1:1 fipscode using "`destfold'bidenbycounty2020.dta" , generate(_mergebiden)
merge 1:1 fipscode using "`destfold'trumpbycounty2020.dta" , generate(_mergetrump)

gen bidenpercent = biden2020/(biden2020+trump2020)*100 

/* Make dummy variable for which counties Chinni & Gimpel code as Military Bastions */

gen mb = 0 
replace mb=1 if fipscode==1051 
replace mb=1 if fipscode==1089 
replace mb=1 if fipscode==2020 
replace mb=1 if fipscode==2068 
replace mb=1 if fipscode==2240 
replace mb=1 if fipscode==8041 
replace mb=1 if fipscode==12009 
replace mb=1 if fipscode==12033
replace mb=1 if fipscode==13053
replace mb=1 if fipscode==13153
replace mb=1 if fipscode==13215
replace mb=1 if fipscode==13245
replace mb=1 if fipscode==16033
replace mb=1 if fipscode==16039
replace mb=1 if fipscode==18121
replace mb=1 if fipscode==20005
replace mb=1 if fipscode==20027
replace mb=1 if fipscode==20041
replace mb=1 if fipscode==20103
replace mb=1 if fipscode==20161
replace mb=1 if fipscode==20209
replace mb=1 if fipscode==21047
replace mb=1 if fipscode==21093
replace mb=1 if fipscode==20015
replace mb=1 if fipscode==22115
replace mb=1 if fipscode==23023
replace mb=1 if fipscode==24003
replace mb=1 if fipscode==24037
replace mb=1 if fipscode==28059
replace mb=1 if fipscode==32009
replace mb=1 if fipscode==36045
replace mb=1 if fipscode==37051
replace mb=1 if fipscode==37085
replace mb=1 if fipscode==37137
replace mb=1 if fipscode==38101
replace mb=1 if fipscode==39113
replace mb=1 if fipscode==40029
replace mb=1 if fipscode==40033
replace mb=1 if fipscode==40047
replace mb=1 if fipscode==40055
replace mb=1 if fipscode==40075
replace mb=1 if fipscode==45055
replace mb=1 if fipscode==45085
replace mb=1 if fipscode==47031
replace mb=1 if fipscode==47127
replace mb=1 if fipscode==47161
replace mb=1 if fipscode==48029
replace mb=1 if fipscode==48485
replace mb=1 if fipscode==51053
replace mb=1 if fipscode==51570
replace mb=1 if fipscode==51650
replace mb=1 if fipscode==51710
replace mb=1 if fipscode==51740
replace mb=1 if fipscode==51810
replace mb=1 if fipscode==53067
label variable bidenpercent "Percent Biden 2020 2-Party Vote Share"
label define mb 0 "Other" 1 "Military Bastion"
label values mb mb 


/* Testing Military Bastion Voting Contrast */
#delimit ;
ttest bidenperc, by(mb)  ;
graph bar (mean) bidenpercent, 
	over(mb) 
	scheme(s1mono) 
	ytitle(Mean Percent Biden 2020 2-Party Vote Share) 
	bar(1, fcolor(gs8%25)) 
	text(1 20 "n = 3100", size(small))
	text(1 80 "n = 53", size(small))
	;


/*The t-test table and visualization were overkill so we didn't include them
*/


